﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SoftSource.Common;
using SoftSource.MVVMC;
using SoftSource.Timecard.Resources;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Views
{
    public sealed partial class EntriesPriorToProjectBillingDateView : UserControl, IView<IEntriesPriorToProjectBillingDateViewModelConsume>
    {
        #region Fields

        Filter<EntryLog> entryFilter;

        #endregion Fields

        public EntriesPriorToProjectBillingDateView()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(EntriesPriorToProjectBillingDateView_Loaded);
        }

        #region Events

        void EntriesPriorToProjectBillingDateView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.LoadIndicator.BusyContent = ApplicationStrings.LoadingMessage;
                this.LoadIndicator.IsBusy = true;

                this.ViewModel.Load // Load the data required to use this ViewModel
                    .Subscribe(unit => this.LoadIndicator.IsBusy = false);

                this.ViewModel.Entries.Subscribe(pcv =>
                    {
                        this.entryFilter = pcv.AttachFilter<EntryLog>();
                        this.entryFilter.Add(entry =>
                            {
                                ProjectSummary summary = ProjectsComboBox.SelectedItem as ProjectSummary;
                                if (summary != null && entry.ProjectID == summary.ProjectID)
                                    return true;
                                return false;
                            });
                    });
            }
        }

        private void ProjectsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.entryFilter.Refresh.TryInvoke();
        }

        #endregion Events

        #region IView<IEntriesPriorToProjectBillingDateViewModelConsume> Members

        public IEntriesPriorToProjectBillingDateViewModelConsume ViewModel
        {
            get { return this.DataContext as IEntriesPriorToProjectBillingDateViewModelConsume; }
            set { this.DataContext = value; }
        }

        #endregion
    }
}
